import { showToast, showLoadingToast } from "vant";
import router from "@/router";
import wondersh5 from "@/assets/js/wondersh5";
// 浏览器判断
const u = navigator.userAgent.toLowerCase();
export default {
	/**
	 * 提示函数
	 * 禁止点击蒙层、显示一秒后关闭
	 */
	toast: function (msg: any) {
		showToast({
			message: msg,
			duration: 1000,
			position: "middle",
			forbidClick: true
		});
	},
	loading: function (msg: any) {
		showLoadingToast({
			message: msg,
			forbidClick: true,
			loadingType: "spinner",
			duration: 0
		});
	},
	QuickVersion: {
		ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
		android: u.indexOf("android") > -1 || u.indexOf("Linux") > -1, // android终端或uc浏览器
		iPhone: u.indexOf("iphone") != -1, // 是否为iPhone或者QQHD浏览器
		iPad: u.indexOf("ipad") > -1, // 是否iPad
		// isWeb: u.indexOf('windows nt') != -1 || (u.indexOf('mac os') != -1 && u.indexOf('ios_appname') == -1) || u.indexOf("chrome") > -1 , //是否PC浏览器 不完善！！！
		isWeb: u.indexOf("windows nt") != -1 || (u.indexOf("mac os") != -1 && u.indexOf("ios_appname") == -1), // 是否PC浏览器 不完善！！！
		isWeixin: u.indexOf("micromessenger") != -1, // 是否微信
		//是否为安卓端
		isApp: u.indexOf("android_medical_community") != -1 || u.indexOf("android_health_hainan") != -1,
		//是否为iOS段
		isIOSApp: u.indexOf("ios_medical_community") != -1 || u.indexOf("ios_health_hainan") != -1,
		isTaurusApp: u.indexOf("taurusapp") != -1, // 是否政务钉钉
		isMobileApp: function (key: any) {
			return u.indexOf(key) != -1;
		}
		/* isWeb : !this.ios && !this.android && !this.iPad && !this.iPhone && !this.isApp && !this.isIE() && !this.isIOSApp
             && !this.isWeixin */
	},

	isPhone: function (pone: any) {
		// 判断是否是手机号码
		const myreg = /^[1][0-9][0-9]{9}$/;
		if (!myreg.test(pone)) {
			return false;
		} else {
			return true;
		}
	},
	init_date: function () {
		// 现在
		const time = new Date();
		const day = ("0" + time.getDate()).slice(-2);
		const year = time.getFullYear();
		const month = ("0" + (time.getMonth() + 1)).slice(-2);
		const hours = time.getHours(); // 获取系统时，
		const minutes = time.getMinutes(); // 分
		const seconds = time.getSeconds(); // 秒
		const today = year + "." + month + "." + day + " " + hours + ":" + minutes + ":" + seconds;

		return today;
	},
	// 是否为纯数字
	isNum: function (val: any) {
		return /^\d+$/.test(val);
	},

	idcardGetinfo(UUserCard: any, num: number, text: boolean) {
		// 根据身份证号获取信息
		if (num == 1) {
			// 获取出生日期
			let birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
			return birth;
		}
		if (num == 2) {
			// 获取性别
			if (text) {
				if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
					return "男"; // 男
				} else {
					return "女"; // 女
				}
			} else {
				if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
					return "1"; // 男
				} else {
					return "2"; // 女
				}
			}
		}
		if (num == 3) {
			// 获取年龄
			let myDate = new Date();
			let month = myDate.getMonth() + 1;
			let day = myDate.getDate();
			let age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
			if (UUserCard.substring(10, 12) < month || (UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day)) {
				age++;
			}
			return age;
		}
	},
	// 判断是否身份证
	isIDcard: function (idcode: any) {
		let reg = /(^\d{15}$)|(^\d{17}(\d|[xX])$)/;
		if (reg.test(idcode) === false) {
			return false;
		}
		return true;
		// var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
		// var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
		// var code = idcode + ''
		// var last = idcode[17] // 最后一位
		// var seventeen = code.substring(0, 17)
		// var arr = seventeen.split('')
		// var len = arr.length
		// var num = 0
		// for (var i = 0; i < len; i++) {
		//   num = num + arr[i] * weight_factor[i]
		// }
		// var resisue = num % 11
		// var last_no = check_code[resisue]
		// var idcard_patter =
		// /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/
		// var format = idcard_patter.test(idcode)
		// return !!(last === last_no && format)
	},
	// 判断是当前值否为对象{}
	isPlainObject: function (o: any) {
		if (o instanceof Array) {
			return "Array";
		} else if (o instanceof Object) {
			return "Object";
		} else {
			return false;
		}
	},
	// 从APP获取token、accessToken、publicKey
	getTokenFromApp: function () {
		let count = 4; // 等任务都结束后，再resolve
		return new Promise<void>((resolve, reject) => {
			// 获取publicKey
			wondersh5.nativeStorage(
				{
					method: "get",
					key: "wondersPublicKey"
				},
				(res: any) => {
					const publicKey = res.wondersPublicKey;
					if (publicKey) {
						localStorage.publicKey = publicKey;
					}
					count--;
					if (count <= 0) {
						resolve();
					}
				}
			);
			// 获取token
			wondersh5.nativeStorage(
				{
					method: "get",
					key: "wondersAuthToken"
				},
				(res: any) => {
					const token = res.wondersAuthToken;
					if (token) {
						localStorage.token = token;
					}
					count--;
					if (count <= 0) {
						resolve();
					}
				}
			);
			// 获取accessToken
			wondersh5.nativeStorage(
				{
					method: "get",
					key: "wondersAccessToken"
				},
				res => {
					const accessToken = res.wondersAccessToken;
					if (accessToken) {
						localStorage.accessToken = accessToken;
					}
					count--;
					if (count <= 0) {
						resolve();
					}
				}
			);
			// 获取用户信息
			wondersh5.nativeStorage(
				{
					method: "get",
					key: "wondersUserInfo"
				},
				res => {
					if (res.data) {
						localStorage.userInfo = JSON.stringify(res.data);
					}
					count--;
					if (count <= 0) {
						resolve();
					}
				}
			);
		});
	},
	// 获取assets静态images资源
	getAssetsImage: function (url: string) {
		return new URL(`../assets/images/${url}`, import.meta.url).href;
	},
	//页面跳转公共路径
	webBaseUrl: function (basePath = "record-print-app") {
		let url = window.location.origin;
		if (basePath) {
			url = url + "/" + basePath;
		}
		return url;
	}
};
